﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SignInServer.Server.Tools;

namespace SignInServer.Server
{
    /// <summary>
    /// 组装用于发送的消息
    /// </summary>
    class PackMessage
    {
        /// <summary>
        /// 该消息包的总大小(信息类别+信息主体)
        /// </summary>
        private byte[] messageSize;
        /// <summary>
        /// 该消息包
        /// </summary>
        private BasicMessage basicMessage;

        public PackMessage()
        {
            basicMessage = new BasicMessage();
        }

        public PackMessage(BasicMessage basicMessage)
        {
            this.basicMessage = basicMessage;
        }

        public PackMessage(int type, byte[] data)
        {
            basicMessage = new BasicMessage(type, data);
        }
        /// <summary>
        /// 获取用于直接发送的数据包
        /// </summary>
        /// <returns></returns>
        public byte[] getPackData()
        {
            byte[] basicMessageBytes = basicMessage.generatedMessage();
            messageSize = ConvertTypeTool.Int32ToByteArray(basicMessageBytes.Length);
            return ConvertTypeTool.PooledByteArrayForPackMessage(messageSize, basicMessageBytes);
        }
        /// <summary>
        /// 获取用于直接发送的数据包
        /// </summary>
        /// <param name="type"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public byte[] getPackData(int type, byte[] data)
        {
            basicMessage = new BasicMessage(type, data);
            byte[] basicMessageBytes = basicMessage.generatedMessage();
            messageSize = ConvertTypeTool.Int32ToByteArray(basicMessageBytes.Length);
            return ConvertTypeTool.PooledByteArrayForPackMessage(messageSize, basicMessageBytes);
        }
        /// <summary>
        /// 获取用于直接发送的数据包
        /// </summary>
        /// <param name="basicMessage"></param>
        /// <returns></returns>
        public byte[] getPackData(BasicMessage basicMessage)
        {
            this.basicMessage = basicMessage;
            byte[] basicMessageBytes = basicMessage.generatedMessage();
            messageSize = ConvertTypeTool.Int32ToByteArray(basicMessageBytes.Length);
            return ConvertTypeTool.PooledByteArrayForPackMessage(messageSize, basicMessageBytes);
        }
    }
}
